﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Diagnostics;
using zevenseas.LifeCycleManagement.Utilities;
using System.Data;

namespace zevenseas.LifeCycleManagement.TimerJobs
{
    public class UnInstallEventHandlerTimerjob : SPJobDefinition
    {
        private ErrorHandling errorHandling;
        
        public UnInstallEventHandlerTimerjob()
        : base()
        {
          errorHandling = new ErrorHandling();
          errorHandling.productName = "UnInstallEventHandlerTimerjob";
            errorHandling.exeName = "zevenseas.LifeCycleManagement.TimerJobs";
        }

        public UnInstallEventHandlerTimerjob(string name, SPWebApplication webApplication, SPServer server, SPJobLockType lockType)
        : base(name, webApplication, server, lockType)
        {
        }

        public override void Execute(Guid contentDbId)
        {
            Guid guid = new Guid("080ed5e5-3791-4433-a668-03b3902178a6");

            SPWebApplication webApplication = this.WebApplication;

            foreach (SPSite site in webApplication.Sites)
            {
                using (site)
                {
                    if (site != null)
                    {
                        foreach (SPWeb subweb in site.AllWebs)
                        {
                            using (subweb)
                            {
                                subweb.Features.Remove(guid, true);
                            }
                        }
                    }
                }
            }
        }
    }

    public class EventHandlerTimerjob : SPJobDefinition
    {
        private ErrorHandling errorHandling;

        [Persisted]
        public bool Installing;

        public int WebsProcessed = 0;
        public int FeatureInstalled = 0;
        public int FeatureAlreadyInstalled = 0;
                       
        public EventHandlerTimerjob()
        : base()
        {
          errorHandling = new ErrorHandling();
          errorHandling.productName = "InstallEventHandlerTimerjob";
            errorHandling.exeName = "zevenseas.LifeCycleManagement.TimerJobs";
        }

        public EventHandlerTimerjob(string name, SPWebApplication webApplication, SPServer server, SPJobLockType lockType)
        : base(name, webApplication, server, lockType)
        {
        }

        public override void Execute(Guid contentDbId)
        {
            //Getting the configuration of the persistence store
            try
            {
                FeatureEventHandler(this.Installing);

            }
            catch (Exception getConfigurationError)
            {
                errorHandling.LogMessage(getConfigurationError.Message.ToString(), getConfigurationError);
            }

            //Storing the gathered data
            EventHandlerStatus status = this.WebApplication.GetChild<EventHandlerStatus>("EventHandlerStatus");
            if (status != null)
            {
                status.Delete();
                //status.Uncache();
                //status.Unprovision();                
            }

            SPPersistedObject parent = this.WebApplication;
            status = new EventHandlerStatus("EventHandlerStatus", parent, Guid.NewGuid());
            status.WebsProcessed = this.WebsProcessed;
            status.FeatureAlreadyInstalled = this.FeatureAlreadyInstalled;
            status.FeatureInstalled = this.FeatureInstalled;
            status.Update();
        }

        protected void FeatureEventHandler(bool Install)
        {
            Guid guid = new Guid("080ed5e5-3791-4433-a668-03b3902178a6");

            SPWebApplication webApplication = this.WebApplication;
            
            foreach (SPSite site in webApplication.Sites)
            {
                using (site)
                {
                    if (site != null)
                    {
                        foreach (SPWeb subweb in site.AllWebs)
                        {
                            using (subweb)
                            {
                                if (Install)
                                {
                                    try
                                    {
                                        subweb.AllowUnsafeUpdates = true;
                                        subweb.Features.Add(guid);
                                        subweb.AllowUnsafeUpdates = false;
                                        this.FeatureInstalled++;
                                    }
                                    catch (Exception installError)
                                    {
                                        errorHandling.LogMessage(installError.Message.ToString(), installError);
                                        this.FeatureAlreadyInstalled++;
                                    }
                                }
                                else
                                {
                                    try
                                    {
                                        SPFeature feature = subweb.Features[guid];
                                        if (feature != null)
                                        {
                                            this.FeatureAlreadyInstalled++;
                                        }
                                    }
                                    catch (Exception checkError)
                                    {
                                        errorHandling.LogMessage(checkError.Message.ToString(), checkError);
                                    }
                                }
                            }
                            this.WebsProcessed++;
                        }
                    }
                }
            }           
        }
    }
}
